//
//  BMScanViewController.m
//  BMScanDemo
//
//  Created by __liangdahong on 2017/4/30.
//  Copyright © http://idhong.com All rights reserved.
//  Copyright © https://github.com/asiosldh/BMScan All rights reserved.
//

#import "BMScanViewController.h"

@interface BMScanViewController () {
    
    NSString *_QRCode;
    NSString *_barCode;
}

@end

@implementation BMScanViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"刷新扫描 UI" style:0 target:self action:@selector(updateScanUIClick)];
}

- (void)updateScanUIClick {
    [self reloadScan];
}

- (void)scanCaptureWithValueString:(NSString *)valueString {
    [super scanCaptureWithValueString:valueString];
    if (self.scanType == BMScanTypeQRCode) {
        if ([RAUtil checkIsBarCodeValue:valueString]) {
            [RAUtil showAlertWithMessage:@"请扫描二维码(非条形码)" alertType:NSRAAlertTypeError];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self startScanning];
            });
        } else {
            _QRCode = valueString;
            [self closureScanning];
            RAFSUCodeModel *fsuCodeModel = [[RAFSUCodeModel alloc] initWithQRCode:_QRCode barCode:_barCode];
            [self.delegate scannedFSUCodeModel:fsuCodeModel];
            [self.navigationController popViewControllerAnimated:YES];
            return;
        }
    } else if (self.scanType == BMScanTypeBarCode) {
        if ([RAUtil checkIsBarCodeValue:valueString]) {
            _barCode = valueString;
            self.scanType = BMScanTypeQRCode;
        } else {
            [RAUtil showAlertWithMessage:@"请扫描条形码(非二维码)" alertType:NSRAAlertTypeError];
        }
        [self updateScanUIClick];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self startScanning];
        });
    }
}

//-  (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//    [NSObject bm_identifyPhotoAlbumQRCodeWithResultsBlock:^(NSArray<NSString *> *codeArray) {
//        NSLog(@"%@", codeArray);
//    }];
//}


- (CGFloat)areaX {
    
    return (SCREEN_WIDTH - self.areaWidth)/2.0;
}

- (CGFloat)areaY {
    return (SCREEN_HEIGHT - self.areaXHeight + NAVIGATION_BAR_HEIGHT + STATUS_BAR_HEIGHT) / 2;
}

- (CGFloat)areaWidth {
    
    return 300 * SCREEN_WIDTH_COEFFIENT;
}

- (CGFloat)areaXHeight {
    
    switch (_scanType) {
        case BMScanTypeBarCode:
            return 150 * SCREEN_WIDTH_COEFFIENT;
        case BMScanTypeQRCode:
            return 300 * SCREEN_WIDTH_COEFFIENT;
            
        default:
            return 300 * SCREEN_WIDTH_COEFFIENT;
    }
}
- (CGFloat)areaTitleDistanceHeight {
    return 25;
}

- (UIColor *)areaColor {
    return [UIColor colorWithRed:0 green:0 blue:0 alpha:arc4random_uniform(30)/100.0];
}
- (UIColor *)feetColor {
    return [UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1];
}

- (UIColor *)scanfLin {
    return [UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1];
}

- (BMScanLinViewAnimation)scanLinViewAnimation {
    return arc4random_uniform(4);
}

- (BMScanLin)scanLin {
    return _scanLinType;
}

- (BMScanType)scanType {
    return _scanType;
}

- (BOOL)hidenLightButton {
    return arc4random_uniform(2);
}

@end
